package com.rayenterprise.kms.widgets;

import java.util.List;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.rayenterprise.kms.client.remote.NotionRemote;
import com.rayenterprise.kms.domain.FinishedStat;

/**
 * This class is used to display:
 * <p>
 * Holders and respective notion numbers
 * </p>
 */

public class HolderSummaryDisplay extends Composite {
	VerticalPanel hPanel = new VerticalPanel();	
	HTML content = new HTML();
	
	JavaScriptObject convertedData;

	public HolderSummaryDisplay() {
		
		hPanel.setBorderWidth(0);
		hPanel.setSpacing(5);
		hPanel.setStyleName("stat-out");
		showCompletePercentage();
		HTMLPanel holder = new HTMLPanel("<div id=\"graphic\"></div>");
		HTMLPanel percent = new HTMLPanel("<div id=\"percent\"></div>");
		
		
		hPanel.add(holder);
		hPanel.add(percent);		
		initWidget(hPanel);
		
	}

	private void showCompletePercentage() {
		
		NotionRemote.Util.getInstance().getNotionCompletePercentage(
				new AsyncCallback<List<FinishedStat>>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(List<FinishedStat> result) {
						int i = 0;	
						NumberFormat nf = NumberFormat.getDecimalFormat();
						String[][] statData = new String[result.size()][3];
						String[][] percent = new String[result.size()][3];
						for (FinishedStat stat : result) {
							
							statData[i][0]= String.valueOf(stat.getCountAll());
							statData[i][1]= stat.getHolderName();
							percent[i][0]= String.valueOf(nf.format(stat.getPercentage()));
							percent[i][1]= stat.getHolderName();
							
							i++;
							
						}
						
						jsGraph(array2Convert(statData));
						jsGraphPercent(array2Convert(percent));
					}

				});
	}

	public static native void jsGraph(JavaScriptObject data) /*-{
		var arrayOfData = data;
		$wnd.$('#graphic').jqBarGraph({ data: arrayOfData ,
										title:'<h3>Number of Each Holder</h3>',
										colors: ['#242424','#437346','#97D95C'] ,
										position:'top'});		

		
	}-*/;
	public static native void jsGraphPercent(JavaScriptObject data) /*-{
		var arrayOfData = data;		
		$wnd.$('#percent').jqBarGraph({ data: arrayOfData ,
										title:'<h3>Complete Percent Each Holder</h3>',
										colors: ['#242424','#437346','#97D95C'] ,
										postfix:' %',
										position:'top'});

	
}-*/;
	
    
	public static JavaScriptObject array2Convert(String[][] array) { 
		JavaScriptObject result = newJS2Array(array.length, array[0]==null ? 0 : array[0].length); 
		for (int i = 0; i < array.length; i++) { 
			for(int j=0; j<array[i].length; j++ ){
				array2Set(result, i, j, array[i][j]);
			}
		} 
		return result; 
	} 

	private static native JavaScriptObject newJS2Array(int i, int j) /*-{ 
    	var multiArray = new Array(i);
    	for(index=0; index<i; index++){
    		multiArray[index] = new Array(j);
    	}
        return multiArray; 
	}-*/; 

	public static native int array2Length(JavaScriptObject array) /*-{ 
	    return array.length; 
	}-*/; 

	public static native String array2Get(JavaScriptObject array, int i, int j) /*-{ 
	    return array[i][j]; 
	}-*/; 
	
	public static native void array2Set(JavaScriptObject array, int i, int j, String value) /*-{ 
	    array[i][j] = value; 
	}-*/; 		

}
